package qst.com.bacK.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import qst.com.bacK.Constant;
import qst.com.bacK.exception.ExamException;
import qst.com.bacK.pojo.entity.Team;
import qst.com.bacK.pojo.entity.TestPersonnel;
import qst.com.bacK.pojo.entity.User;
import qst.com.bacK.service.TestPersonnelService;
import qst.com.bacK.service.UserService;
import qst.com.bacK.service.WeatherService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * Author:   msh
 * Date:     2024/10/14 上午9:18
 * Description:
 */

@Controller
public class LoginController {
    @Autowired
    private UserService userService;
    @Autowired
    private TestPersonnelService testPersonnelService;
    @Autowired
    private WeatherService weatherService;

    @GetMapping("/showAllTeam")
    public String showAllTeam(Model model) {
        List<Team> teamList = userService.showAllTeam();
        model.addAttribute("teamList", teamList);
        return "forward:/user/reg.jsp";
    }

    // 更改密码
    @PostMapping("/password.action")
    public String changePassword(HttpSession session,
                                 @RequestParam("oldPassword") String oldPasswd,
                                 @RequestParam("newPassword") String newPasswd,
                                 @RequestParam("confirm") String confirm) {
        User user = (User) session.getAttribute(Constant.CURRENT_USER);

        if (!confirm.equals(newPasswd)) {
            session.setAttribute("_error_key", "两次密码不一致");
            return "redirect:/password.jsp";
        } else {
            try {
                userService.updatePassword(user.getId(), oldPasswd, newPasswd);
                session.setAttribute("_message_key", "密码修改成功,请退出重新登录");
                return "redirect:/passwordmsg.jsp";
            } catch (ExamException e) {
                session.setAttribute("_error_key", e.getMessage());
                return "redirect:/password.jsp";
            }
        }
    }

    @GetMapping("/logout.action")
    public String logout(HttpSession session) {
        session.removeAttribute(Constant.CURRENT_USER);
        return "redirect:/login.jsp";
    }

    // 登录
    @PostMapping("/login")
    public ModelAndView login(
            @RequestParam("login") String login,
            @RequestParam("passwd") String passwd,
            @RequestParam("menu") Integer menu,
            HttpSession session,
            HttpServletRequest req) throws IOException {

        try {
            User user = userService.login(login, passwd);
            session.setAttribute(Constant.CURRENT_USER, user);
            Map weather = weatherService.queryWeather("东莞");
            req.getSession().setAttribute("weather", weather);

            if (user.getType() == 4) {
                TestPersonnel personnel = testPersonnelService.findById(user.getId());
                session.setAttribute(Constant.CURRENT_TESTPERSONNEL, personnel);
            }

            if (menu == 0) {
                return new ModelAndView("redirect:/frame.jsp");
            } else {
                return new ModelAndView("redirect:/index.jsp");
            }
        } catch (ExamException ex) {
            session.setAttribute("_error_key", ex.getMessage());
            session.setAttribute("login", login);
            return new ModelAndView("redirect:/login.jsp");
        }
    }

    // 更改密码页面
    @GetMapping("/password.action")
    public String changePassword() {
        return "forward:/password.jsp";
    }
}
